package com.leetcode.partition8;

/**
 * @author `RKC`
 * @date 2021/10/23 10:23
 */
public class LC713乘积小于K的子数组 {

    public static int numSubarrayProductLessThanK(int[] nums, int k) {
        return slidingWindow(nums, k);
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        int k = 0;
        System.out.println(numSubarrayProductLessThanK(nums, k));
    }

    private static int slidingWindow(int[] nums, int k) {
        int left = 0, answer = 0, product = 1;

        for (int right = 0; right < nums.length; right++) {
            product *= nums[right];
            while (product >= k && left <= right) {
                product /= nums[left++];
            }
            //如果一个子串的乘积小于k，那么他的每个子集都小于k，在范围[left, right]区间内，一共有right-left+1个元素
            answer += right - left + 1;
        }
        return answer;
    }
}
